package com.dj.uphome.web.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dj.common.web.service.impl.BaseCrudServiceImpl;
import com.dj.uphome.web.mapper.system.SysMenuMapper;
import com.dj.uphome.web.service.system.SysMenuService;
import com.dj.uphome.web.vo.system.SysMenuVo;

@Service
public class SysMenuServiceImpl  extends BaseCrudServiceImpl<SysMenuVo> implements SysMenuService {
	@Autowired
	private SysMenuMapper sysMenuMapper;
	@Override
	public List<SysMenuVo> listAllByUserId(int userId) throws Exception {
		return sysMenuMapper.listAllByUserId(userId);
	}
	@Override
	public List<SysMenuVo> listAllForAuthorize(int roleId) throws Exception {
		return sysMenuMapper.listAllForAuthorize(roleId);
	}

	@Override
	public List<SysMenuVo> listTree() throws Exception {
		SysMenuVo vo = new SysMenuVo();
		vo.setParentId(-1);
		List<SysMenuVo> menuList = sysMenuMapper.findAll(vo);

		List<SysMenuVo> parentMenus = new ArrayList<SysMenuVo>();
		for (SysMenuVo menu : menuList) {
			for (SysMenuVo menu_old : menuList) {
				if (menu.getMenuId() == menu_old.getParentId()) {
					List<SysMenuVo> menus = menu.getSubMenu();
					if (null == menus) {
						menus = new ArrayList<SysMenuVo>();
					}
					menu_old.setIconCls(menu_old.getMenuIcon());
					menus.add(menu_old);
					menu.setSubMenu(menus);
					menu.setChildren(menus);
				}
			}
			if (menu.getParentId() == 0) {
				parentMenus.add(menu);
			}
		}
		return parentMenus;
	}

	@Override
	public List<SysMenuVo> findAll(SysMenuVo vo) throws Exception {
		return sysMenuMapper.findAll(vo);
	}

	@Override
	public List<SysMenuVo> findListByRoleId(int roleId) throws Exception {
		return sysMenuMapper.findListByRoleId(roleId);
	}

}
